﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;



public class AppConfigTemplatePlugin : IPlugin
{
    private List<ITemplate> _templates = new List<ITemplate>();
    const string PluginName = "AppConfigTemplate gneratator  Plugin";
    const string PluginDescription = "AppConfigTemplate Generator Plugin";
    public AppConfigTemplatePlugin()
    {
        _templates.Add(new AppConfigTemplate());
    }
    public string Description
    {
        get { return PluginName; }
    }

    public string Name
    {
        get { return PluginDescription; }
    }

    public System.Collections.Generic.List<ITemplate> Templates
    {
        get { return _templates; }
        set { _templates = value; }
    }

    public class AppConfigTemplate : ITemplate
    {

        const string TemplateName = "AppConfigTemplate Generator Template";
        const string TemplateDescription = "AppConfigTemplate Generator Template";
        const string TemplateOutputLanguage = "xml";
        private ArrayList _arraylist = new ArrayList();
        private string _workingdir;
        private string _languageMappingFileName;
        private string _dbTargetMappingFileName;
        private const string GENERATED_FILE_NAME = "App.config";
        private const string GENERATED_FOLDER_NAME = "\\";

        private ArrayList _refkeyList = new ArrayList();
        private bool ExecuteLocal(ref MyMeta.IDatabase db, string workingDir, GenerationProject generationProject)
        {
            try
            {
                MyMeta.IDatabase _dataBase = db;

                _workingdir = workingDir;
                System.Text.StringBuilder Output = new System.Text.StringBuilder();


                //Output.AppendLine("--Script Generated By OpenORM.NET at :" + DateTime.Now.ToLongDateString());
                //Output.AppendLine("--Plugin Version: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString());
                //Output.AppendLine("");
                
                Output.AppendLine("<?xml version=" + System.Convert.ToChar(34) + "1.0" + System.Convert.ToChar(34) + " encoding=" + System.Convert.ToChar(34) + "utf-8" + System.Convert.ToChar(34) + " ?>") ;
                Output.AppendLine("<configuration>") ;
                Output.AppendLine("  <appSettings>") ;

                switch (db.Root.Driver)
                {
                    case MyMeta.dbDriver.Access:
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "AdoNetAssemblyName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "AdoNetConnectionTypeName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "System.Data.OleDb.OleDbConnection" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "AdoNetCommandTypeName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "System.Data.OleDb.OleDbCommand" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "AdoNetCommandTimeOut" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "0" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "ParameterPrefix" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "@" + System.Convert.ToChar(34) + "/> <!--SQL Server=@; SQLite=$; MySql=@; Oracle=:;Firebird=:;Postgresql=$;Access=@ --> ");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "PkFunction" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "SET @newId = @@Identity " + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("   </appSettings>");
                        Output.AppendLine("   <connectionStrings>");
                        Output.AppendLine("     <add  name=" + System.Convert.ToChar(34) + generationProject.ConnectionStringName + System.Convert.ToChar(34) + " connectionString=" + System.Convert.ToChar(34) + "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=[Path]Nwind.mdb;Persist Security Info=False" + System.Convert.ToChar(34) + "/>");
                        break;
                    case MyMeta.dbDriver.Advantage:
                        break;
                    case MyMeta.dbDriver.DB2:
                        break;
                    case MyMeta.dbDriver.Firebird:
                        break;
                    case MyMeta.dbDriver.ISeries:
                        break;
                    case MyMeta.dbDriver.Interbase:
                        break;
                    case MyMeta.dbDriver.MySql:
                    case MyMeta.dbDriver.MySql2:
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "AdoNetAssemblyName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "MySql.Data, Version=6.9.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "AdoNetConnectionTypeName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "MySql.Data.MySqlClient.MySqlConnection" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "AdoNetCommandTypeName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "MySql.Data.MySqlClient.MySqlCommand" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "AdoNetCommandTimeOut" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "0" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "ParameterPrefix" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "@" + System.Convert.ToChar(34) + "/> <!--SQL Server=@; SQLite=$; MySql=@; Oracle=:;Firebird=:;Postgresql=$;Access=@ --> ");
                        Output.AppendLine("     <add key=" + System.Convert.ToChar(34) + "PkFunction" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "SET @newId = @@LAST_INSERT_ID(); " + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("   </appSettings>");
                        Output.AppendLine("   <connectionStrings>");
                        Output.AppendLine("     <add  name=" + System.Convert.ToChar(34) + generationProject.ConnectionStringName + System.Convert.ToChar(34) + " connectionString=" + System.Convert.ToChar(34) + "server=127.0.0.1;uid=root;pwd=12345;database=" + generationProject.SelectedDatabase  + System.Convert.ToChar(34) + "/>");

                        break;
                    case MyMeta.dbDriver.None:
                        break;
                    case MyMeta.dbDriver.Oracle:
                        break;
                    case MyMeta.dbDriver.Pervasive:
                        break;
                    case MyMeta.dbDriver.Plugin:
                        break;
                    case MyMeta.dbDriver.PostgreSQL:
                        break;
                    case MyMeta.dbDriver.PostgreSQL8:
                        break;
                    case MyMeta.dbDriver.SQL:
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "AdoNetAssemblyName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "AdoNetConnectionTypeName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "System.Data.SqlClient.SqlConnection" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "AdoNetCommandTypeName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "System.Data.SqlClient.SqlCommand" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "AdoNetCommandTimeOut" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "0" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "ParameterPrefix" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "@" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "PkFunction" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "SET @newId = SCOPE_IDENTITY()" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("  </appSettings>");
                        Output.AppendLine("  <connectionStrings>");
                        Output.AppendLine("    <add  name=" + System.Convert.ToChar(34) + generationProject.ConnectionStringName + System.Convert.ToChar(34) + " connectionString=" + System.Convert.ToChar(34) + "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=" + generationProject.SelectedDatabase + ";Data Source=localhost;" + System.Convert.ToChar(34) + "/>");

                        break;
                    case MyMeta.dbDriver.SQLite:
                        Output = new System.Text.StringBuilder();
                        Output.AppendLine("<?xml version=" + System.Convert.ToChar(34) + "1.0" + System.Convert.ToChar(34) + "?>");
                        Output.AppendLine("<configuration>");
                        Output.AppendLine("  <startup useLegacyV2RuntimeActivationPolicy=" + System.Convert.ToChar(34) + "true" + System.Convert.ToChar(34) + ">");
                        Output.AppendLine("    <supportedRuntime version=" + System.Convert.ToChar(34) + "v4.0" + System.Convert.ToChar(34) + " sku=" + System.Convert.ToChar(34) + ".NETFramework,Version=v4.0" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("  </startup>");
                        Output.AppendLine("  <startup>");
                        Output.AppendLine("    <supportedRuntime version=" + System.Convert.ToChar(34) + "v4.0" + System.Convert.ToChar(34) + " sku=" + System.Convert.ToChar(34) + ".NETFramework,Version=v4.0" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("  </startup>");
                        Output.AppendLine("  <appSettings>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "AdoNetAssemblyName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "System.Data.SQLite, Version=1.0.98.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "AdoNetConnectionTypeName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "System.Data.SQLite.SQLiteConnection" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "AdoNetCommandTypeName" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "System.Data.SQLite.SQLiteCommand" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "AdoNetCommandTimeOut" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "0" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "ParameterPrefix" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "@" + System.Convert.ToChar(34) + "/><!--SQL Server=@; SQLite=$; MySql=@; Oracle=:;Firebird=:;Postgresql=$;Access=@ -->");
                        Output.AppendLine("    <add key=" + System.Convert.ToChar(34) + "PkFunction" + System.Convert.ToChar(34) + " value=" + System.Convert.ToChar(34) + "" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine("  </appSettings>");
                        Output.AppendLine("  <connectionStrings>");
                        Output.AppendLine("    <add name=" + System.Convert.ToChar(34) + generationProject.ConnectionStringName + System.Convert.ToChar(34) + " connectionString=" + System.Convert.ToChar(34) + "Provider=SQLite Provider; Persist Security Info=False; Data Source=D:\tk3.dat;Mode=Read;" + System.Convert.ToChar(34) + "/>");
                        Output.AppendLine(" ");

                        break;
                    case MyMeta.dbDriver.VistaDB:
                        break;
                    default:
                        break;
                }

                Output.AppendLine("  </connectionStrings>") ;
                Output.AppendLine("</configuration>") ;

                Output.AppendLine("");
                if (!_workingdir.EndsWith("\\"))
                    _workingdir += "\\";
                System.IO.Directory.CreateDirectory(_workingdir);
                string path = _workingdir + GENERATED_FILE_NAME;
                System.IO.StreamWriter sw = new System.IO.StreamWriter(path);


                sw.Write(Output.ToString());
                sw.Close();

                if (OnFileGenerated != null)
                {
                    OnFileGenerated(path);
                }
                return true;

            }
            catch (Exception ex)
            {
                if (OnException != null)
                {
                    OnException(ex);
                }
                return false;
            }
        }

        public string Name
        {
            get { return TemplateName; }
        }


        public event OnExceptionEventHandler OnException;

        public string WorkingDir
        {
            get { return _workingdir; }
            set { _workingdir = value; }
        }

        public string Description
        {
            get { return TemplateDescription; }
        }

        public event OnInfoEventHandler OnInfo;


        public string OutputLanguage
        {
            get { return TemplateOutputLanguage; }
        }

        public event OnFileGeneratedEventHandler OnFileGenerated;

        public event OnPercentDoneEventHandler OnPercentDone;

        public string DbTargetMappingFileName
        {
            get { return _dbTargetMappingFileName; }
            set { _dbTargetMappingFileName = value; }
        }

        public string LanguageMappingFileName
        {
            get { return _languageMappingFileName; }
            set { _languageMappingFileName = value; }
        }

        public string GUID
        {
            get { return "18995d7-d79f-462c-8v51-4588991d42al"; }
        }

        #region ITemplate Members


        public bool Execute(MyMeta.IDatabase db, string workingDir, GenerationProject generationProject)
        {
            return this.ExecuteLocal(ref db, workingDir, generationProject);
        }

        #endregion
    }
    // End class FKViewsBuilder

}